package net.mihome.app.ui.view;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.http.Header;

import com.tencent.mm.sdk.platformtools.Log;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import net.aydx84318.weather.bean.WeatherAsyncHttpResponseHandler;
import net.aydx84318.weather.bean.WeatherInfo;
import net.aydx84318.weather.bean.WeatherUtils;
import net.aydx84318.weather.ui.WeatherActivity;
import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.adapter.ListViewNewsAdapter;
import net.mihome.app.adapter.ListViewNewsTypeAdapter;
import net.mihome.app.bean.News;
import net.mihome.app.bean.NewsList;
import net.mihome.app.bean.NewsType;
import net.mihome.app.common.UIHelper;
import net.mihome.app.ui.MihomeNewsListActivity;
import net.mihome.app.utils.PreferenceUtils;
import net.mihome.app.widget.LoadingDialog;
import net.mihome.app.widget.PullToRefreshListView;
import net.mihome.app.R;

public class MihomeZixunTypeView extends XListView {
	private Handler handler;
	private ListViewNewsTypeAdapter adapter;
	private Context context;
	private List<NewsType> lvNewsData = new ArrayList<NewsType>();
	private AppContext appContext;
	private int begin = 0;
	private int num = 10;
	private int newsTypeId = NewsList.CATALOG_ALL;
//	private LoadingDialog loadingDialog;
	private WeatherInfo weatherInfo;
	//
	private final static int HANDLER_SHOW_LOADING=111;
	private final static int HANDLER_CLOSE_LOADING=222;
	private MyViewLinster myViewLinster;
	//
	public MihomeZixunTypeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
		Thread.currentThread().setUncaughtExceptionHandler(
				new UncaughtExceptionHandler() {
					@Override
					public void uncaughtException(Thread arg0, Throwable arg1) {
						// TODO Auto-generated method stub
						Log.d("hello", arg0.getName());
						Log.d("hello2", arg1.getLocalizedMessage());
						arg1.printStackTrace();
					}
				});
		afterInit();
		// TODO Auto-generated constructor stub
	}

	public MihomeZixunTypeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		afterInit();
		// TODO Auto-generated constructor stub
	}

	private void afterInit() {
		adapter = new ListViewNewsTypeAdapter(getContext(), lvNewsData,
				R.layout.news_type_listitem);
//		loadingDialog=new LoadingDialog(this.context);
//		loadingDialog.setContentView(R.layout.loadingdialog);
		Handler.Callback callback = new Handler.Callback() {
			@Override
			public boolean handleMessage(Message msg) {
				switch (msg.what) {
				case 1: {
					if (msg.obj != null) {
						lvNewsData.clear();
						lvNewsData
								.addAll((Collection<? extends NewsType>) msg.obj);
					}
					break;
				}
				case 2: {
					if (msg.obj != null) {
						lvNewsData.clear();
						lvNewsData.addAll(0,
								(Collection<? extends NewsType>) msg.obj);
					}
					break;
				}
				case 3: {
					if (msg.obj != null) {
						lvNewsData
								.addAll((Collection<? extends NewsType>) msg.obj);
					}
					break;
				}
				case HANDLER_SHOW_LOADING:{
//					if(!loadingDialog.isShowing()){
//						loadingDialog.show();
//					}
					break;
				}
				case HANDLER_CLOSE_LOADING:{
//					if(loadingDialog.isShowing()){
//						loadingDialog.dismiss();
//					}
					break;
				}
				default:
					break;
				}
				adapter.notifyDataSetChanged();
				stopRefresh();
				stopLoadMore();
				setRefreshTime("不久前");
				return false;
			}
		};
		handler = new Handler(callback);
		setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				
				if(arg2==1){
					Intent intent=new Intent(view.getContext(),WeatherActivity.class);
					intent.putExtra("weatherInfo",weatherInfo);
					view.getContext().startActivity(intent);
				}else{
					NewsType news = null;
					// 判断是否是TextView
					if (view instanceof TextView) {
						news = (NewsType) view.getTag();
					} else {
						TextView tv = (TextView) view
								.findViewById(R.id.news_listitem_title);
						news = (NewsType) tv.getTag();
					}
					if (news == null)
						return;
					// 跳转到新闻详情
					// UIHelper.showNewsRedirect(view.getContext(), news);
					Intent intent = new Intent(view.getContext(),
							MihomeNewsListActivity.class);
					intent.putExtra("newsType", news);
					view.getContext().startActivity(intent);
				}
			}
		});
		setPullLoadEnable(true);
		setPullRefreshEnable(true);
		setAdapter(adapter);
		appContext = (AppContext) getContext().getApplicationContext();
		setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				new Thread(new Runnable() {
					@Override
					public void run() {
						Message msMessage = new Message();
						try {
							msMessage.what = 2;
							begin = 0;
							num = 10;
							List<NewsType> list = appContext.getNewsTypeList(
									begin, num);
							if (list != null && !list.isEmpty()
									&& getVisibility() == View.VISIBLE) {
								// lvNewsData.clear();
								// lvNewsData.addAll(0,list);
								msMessage.obj = list;
							}
						} catch (AppException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						handler.sendEmptyMessage(HANDLER_CLOSE_LOADING);
						handler.sendMessage(msMessage);
					}
				}).start();
			}

			@Override
			public void onLoadMore() {
				new Thread(new Runnable() {
					@Override
					public void run() {
						Message msgMessage = new Message();
						msgMessage.what = 3;
						try {
							begin = (begin + 1) * num;
							List<NewsType> list = appContext.getNewsTypeList(
									begin, num);
							if (list != null && !list.isEmpty()
									&& getVisibility() == View.VISIBLE) {
								// lvNewsData.addAll(list);
								msgMessage.obj = list;
							}
						} catch (AppException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						handler.sendEmptyMessage(HANDLER_CLOSE_LOADING);
						handler.sendMessage(msgMessage);
					}
				}).start();
			}
		});
	}

	public void initData() {
//		if(loadingDialog!=null&&!loadingDialog.isShowing()){
//			loadingDialog.show();
//		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msMessage = new Message();
				msMessage.what = 1;
				try {
					begin = 0;
					num = 10;
					List<NewsType> list = appContext
							.getNewsTypeList(begin, num);
					if (list != null && !list.isEmpty()
							&& getVisibility() == View.VISIBLE) {
						msMessage.obj = list;
					}
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				handler.sendEmptyMessage(HANDLER_CLOSE_LOADING);
				handler.sendMessage(msMessage);
			}
		}).start();
	}


}
